package com.wei.czz.framework.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wei.czz.common.enums.CommonEnum;
import com.wei.czz.common.enums.ResultEnum;
import com.wei.czz.common.exception.CzzException;
import com.wei.czz.common.po.UserPo;
import com.wei.czz.common.tool.PageDto;
import com.wei.czz.common.tool.PageQuery;
import com.wei.czz.common.utils.SecurityUtils;
import com.wei.czz.common.vo.common.BaseVo;
import com.wei.czz.framework.common.dao.LimitConfigFlowDao;
import com.wei.czz.framework.common.entity.LimitConfigFlowEntity;
import com.wei.czz.framework.common.service.LimitConfigFlowService;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2023-02-16 21:38:02
 * className: LimitConfigServiceImpl 限流请求服务接口实现类
 * version: 1.0
 * description:
 */
@Service("limitConfigFlowService")
@AllArgsConstructor
public class LimitConfigFlowServiceImpl extends ServiceImpl<LimitConfigFlowDao, LimitConfigFlowEntity>
        implements LimitConfigFlowService {

    private static final Logger log = LoggerFactory.getLogger(LimitConfigFlowServiceImpl.class);

    @Override
    public void saveOrEdit(LimitConfigFlowEntity limitConfigFlow) {
        Long id = limitConfigFlow.getId();
        if (Objects.isNull(id)) {

            int count = baseMapper.insert(limitConfigFlow);
            log.info("插入限流配置流水完成。count={}", count);
        } else {

            int count = baseMapper.updateById(limitConfigFlow);
            log.info("修改限流配置流水完成。count={}", count);
        }
    }

    @Override
    public PageDto<LimitConfigFlowEntity> getPageList(Long masterId, BaseVo baseVo) {

        // 构造mp分页对象
        Page<LimitConfigFlowEntity> page = PageQuery.initPage(baseVo);

        LambdaQueryWrapper<LimitConfigFlowEntity> limitConfigFlowLambdaUpdateWrapper = new LambdaQueryWrapper<>();
        limitConfigFlowLambdaUpdateWrapper.eq(LimitConfigFlowEntity::getMasterId, masterId)
                .eq(LimitConfigFlowEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 分页查询
        baseMapper.selectPage(page, limitConfigFlowLambdaUpdateWrapper);
        log.info("分页查询限流配置流水完成。list.size={} totalNum={}", page.getRecords().size(), page.getTotal());

        return new PageDto<>(page);
    }

    @Override
    public void resetUse(Long masterId) {
        // 获取操作用户
        UserPo userPo = SecurityUtils.getUser();
        Date date = new Date();

        LambdaUpdateWrapper<LimitConfigFlowEntity> limitConfigFlowLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        limitConfigFlowLambdaUpdateWrapper.set(LimitConfigFlowEntity::getStatus, CommonEnum.ONE.getValue())
                .set(LimitConfigFlowEntity::getUpdateTime, date)
                .set(LimitConfigFlowEntity::getUpdateUser, userPo.getUsername())
                .set(LimitConfigFlowEntity::getUpdateUserId, userPo.getUserId());
        limitConfigFlowLambdaUpdateWrapper.eq(LimitConfigFlowEntity::getMasterId, masterId)
                .eq(LimitConfigFlowEntity::getStatus, CommonEnum.ZERO.getValue())
                .eq(LimitConfigFlowEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 更新
        int count = baseMapper.update(limitConfigFlowLambdaUpdateWrapper);
        log.info("重置限流配置流水状态完成。count={}", count);
    }

    @Override
    public LimitConfigFlowEntity get(Long id) {
        LimitConfigFlowEntity limitConfigFlow = baseMapper.selectById(id);
        if (Objects.isNull(limitConfigFlow)) {
            log.info("限流配置流水不存在");
            throw new CzzException(ResultEnum.ILLEGAL_FAIL.getCode(), "限流配置流水不存在，请确认");
        }
        if (CommonEnum.ONE.getValue().equals(limitConfigFlow.getDeleteStatus())) {
            log.info("限流配置流水已删除");
            throw new CzzException(ResultEnum.REQUEST_FAIL.getCode(), "限流配置流水不存在，请确认");
        }
        return limitConfigFlow;
    }

}